import 'dart:convert';

import 'package:flui/flui.dart';
import 'package:flutter/material.dart';
import 'package:fzcgx_app/network/http.dart';
import 'package:fzcgx_app/ui/base/base_view_model.dart';
import 'package:fzcgx_app/ui/my/factory/model/factory_product_category_model.dart';
import 'package:fzcgx_app/ui/my/factory/model/select_factory_products2_model.dart';

class SelectFactoryProducts2ViewModel extends BaseViewModel {
  List<SelectFactoryProducts2Model> models =
      List<SelectFactoryProducts2Model>();
  String pId;
  List<FactoryProductCategoryModel> selectModels =
      List<FactoryProductCategoryModel>();
  SelectFactoryProducts2ViewModel(this.pId) {
    this.setState(ViewState.Loading);
  }
  void requestApi() {
    Http.instance.postList<SelectFactoryProducts2Model>(
        'pro-cate/child-list', {'pid': this.pId}, onSuccess: (data) {
      if (data != null) {
        this.models = data;
        this.selectModels.clear();
        getSelectModes();
        this.setState(ViewState.Success);
      } else {
        this.setState(ViewState.Empty);
      }
    }, onError: (error) {
      this.setState(ViewState.Error);
    }, onFail: (fail) {
      this.setState(ViewState.Error);
    });
  }

  void onSelectChip(FactoryProductCategoryModel model) {
    model.check = !model.check;
    notifyListeners();
    this.selectModels.clear();
    getSelectModes();
  }

  void onNextStep(BuildContext context) {
    if (this.selectModels.length == 0) {
      FLToast.showText(text: '请选择您有的产品款式');
      return;
    }
     var dismiss = FLToast.loading(text: '正在保存...');
     Http.instance.post('pro-cate/check-save', {'ids':_getSelectModelsStr()},onSuccess: (data){
        FLToast.showSuccess(text:'保存成功');
        Navigator.of(context)..pop()..pop();
     },onError: (error){

     },onFail: (fail){

     },complete: (){
       dismiss();
     });
  }
  String _getSelectModelsStr() {
    List list = List();
    this.selectModels.forEach((element) {
      Map data={'id':element.id,};
      list.add(data);
    });
    return jsonEncode(list);
  }

  void onInput(SelectFactoryProducts2Model model) {
    model.isInput = !model.isInput;

    notifyListeners();
  }

  void onAddFactoryProducts(SelectFactoryProducts2Model model) {
    if (model.nameController.text.toString().isEmpty) {
      // FLToast.showText(text: '请输入款式名称');
      return;
    }
    var dismiss = FLToast.loading(text: '正在添加...');
    Http.instance.post('pro-cate/add', {
      'pid': model.id,
      'name': model.nameController.text,
    }, onSuccess: (data) {
      model.nameController.text = '';
      FLToast.showSuccess(text: '添加成功');
      dismiss();
      requestApi();
    }, onError: (error) {
      dismiss();
    }, onFail: (fail) {
      dismiss();
    },complete: (){
      
    });
  }

  void getSelectModes() {
    this.models.forEach((element) {
      element.childs.forEach((model) {
        if (model.check) {
          this.selectModels.add(model);
        }
      });
    });
    notifyListeners();
  }
}
